home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SOCKUTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  3.2 KB  |  162 lines

  1. /* Low level socket routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "usock.h"
  8.  
  9. #if !defined(_lint)
  10. static char rcsid[] OPTIONAL = "$Id: sockutil.c,v 1.15 1997/08/19 01:19:22 root Exp root $";
  11. #endif
  12.  
  13. /* Convert a socket (address + port) to an ascii string of the form
  14.  * aaa.aaa.aaa.aaa:ppppp
  15.  */
  16. char *
  17. psocket(p)
  18. void *p;    /* Pointer to structure to decode */
  19. {
  20. #if    defined(AX25) || defined(NETROM)
  21.     char tmp[11];
  22. #endif
  23.     static char buf[30];
  24.     union sp sp;
  25.     struct socket thesocket;
  26.  
  27.     sp.p = p;
  28.     switch(sp.sa->sa_family){
  29.     case AF_LOCAL:
  30.         buf[0] = '\0';
  31.         break;
  32.     case AF_INET:
  33.         thesocket.address = sp.in->sin_addr.s_addr;
  34.         thesocket.port = sp.in->sin_port;
  35.         strncpy (buf, pinet(&thesocket), 30);
  36.         break;
  37. #ifdef    AX25
  38.     case AF_AX25:
  39.         (void) pax25(tmp,sp.ax->ax25_addr);
  40.         if(strlen(sp.ax->iface) != 0)
  41.             sprintf(buf,"%s on port %s",tmp,sp.ax->iface);
  42.         else
  43.             strncpy (buf, tmp, 30);
  44.         break;
  45. #endif
  46. #ifdef    NETROM
  47.     case AF_NETROM:
  48.         (void) pax25(tmp,sp.nr->nr_addr.user);
  49.         sprintf(buf,"%s @ ",tmp);
  50.         (void) pax25(tmp,sp.nr->nr_addr.node);
  51.         strcat(buf,tmp);
  52.         break;
  53. #endif
  54.     default:
  55.         break;
  56.     }
  57.     return buf;
  58. }
  59.  
  60. /* Return ASCII string giving reason for connection closing */
  61. const char *
  62. sockerr(s)
  63. int s;    /* Socket index */
  64. {
  65.     register struct usock *up;
  66.  
  67.     if((up = itop(s)) == NULLUSOCK){
  68.         errno = EBADF;
  69.         return Badsocket;
  70.     }
  71.     switch(up->type){
  72. #ifdef    AX25
  73.     case TYPE_AX25I:
  74.         if(up->cb.ax25 != NULLAX25)
  75.             return NULLCHAR;    /* nothing wrong */
  76.         return Axreasons[(int)up->errcodes[0]];
  77. #endif
  78. #ifdef    NETROM
  79.     case TYPE_NETROML4:
  80.         if(up->cb.nr4 != NULLNR4CB)
  81.             return NULLCHAR;    /* nothing wrong */
  82.         return Nr4reasons[(int)up->errcodes[0]];
  83. #endif
  84.     case TYPE_TCP:
  85.         if(up->cb.tcb != NULLTCB)
  86.             return NULLCHAR;    /* nothing wrong */        
  87.         return Tcpreasons[(int)up->errcodes[0]];
  88.     default:
  89.         errno = EOPNOTSUPP;    /* not yet, anyway */
  90.         return NULLCHAR;
  91.     }
  92. }
  93. /* Get state of protocol. Valid only for connection-oriented sockets. */
  94. const char *
  95. sockstate(s)
  96. int s;        /* Socket index */
  97. {
  98.     register struct usock *up;
  99.  
  100.     if((up = itop(s)) == NULLUSOCK){
  101.         errno = EBADF;
  102.         return NULLCHAR;
  103.     }
  104.     if(up->cb.p == NULLCHAR){
  105.         errno = ENOTCONN;
  106.         return NULLCHAR;
  107.     }
  108.     switch(up->type){
  109.     case TYPE_TCP:
  110.         return Tcpstates[(int)up->cb.tcb->state];
  111. #ifdef    AX25
  112.     case TYPE_AX25I:
  113.         return Ax25states[up->cb.ax25->state];
  114. #endif
  115. #ifdef    NETROM
  116.     case TYPE_NETROML4:
  117.         return Nr4states[up->cb.nr4->state];
  118. #endif
  119.     default:
  120.         /* Datagram sockets don't have state */
  121.         errno = EOPNOTSUPP;
  122.         return NULLCHAR;
  123.     }
  124.     /*NOTREACHED*/
  125. }
  126. /* Convert a socket index to an internal user socket structure pointer */
  127. struct usock *
  128. itop(s)
  129. register int s;    /* Socket index */
  130. {
  131.     register struct usock *up;
  132.  
  133.     s -= SOCKBASE;
  134.     if(s < 0 || s >= Nusock)
  135.         return NULLUSOCK;
  136.  
  137.     up = &Usock[s];
  138.  
  139.     if(up->type == NOTUSED)
  140.         return NULLUSOCK;
  141.     return up;
  142. }
  143.  
  144. #ifdef MSDOS
  145. void
  146. st_garbage(red)
  147. int red;
  148. {
  149.     int i;
  150.     struct usock *up;
  151.  
  152.     for(i=SOCKBASE;i<SOCKBASE+Nusock;i++){
  153.         /* FIXED!!! up = &Usock[i]; */
  154.         up = itop(i);
  155.         if(up->type == TYPE_LOCAL_STREAM)
  156.             mbuf_crunch(&up->cb.local->q);
  157.     }
  158. }
  159. #endif
  160.  
  161.  
  162.